//
//  UIBarButtonItem+Extension.swift
//  Wei91BoiOS
//
//  Created by bing on 16/3/24.
//  Copyright © 2016年 bing. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    // 扩展里面只能添加便利构造函数，便利构造函数必须调用指定的构造函数来创建对象，也可以调用便利构造函数，实际上内容他调用了指定构造函数
    convenience init(imageName: String, target: AnyObject, action: Selector) {
        // 1.自己调用指定构造函数
        let button = UIButton()
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        // 点击添加事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        self.init(customView: button)
    }
}